<?
   class Database
   {
     var $host;
     var $user;
     var $password;
     var $dbc;
	   var $db;

     function Database($host,$user,$password,$dbc)
     {
        $this->host = $host;
        $this->password = $password;
        $this->user = $user;
        $this->dbc = $dbc;
        $this->db=mysql_connect($this->host,$this->user,$this->password,true) or die (mysql_error());
        //mysql_select_db($this->dbc,$this->db) or die('Error');
        mysql_select_db($this->dbc,$this->db) or die (mysql_error());
     }

      function RunQuery($query)
      {
         $result = mysql_query($query) or die ('DB Error');
 		 $res = $result;
		// mysql_free_result();
	//	 mysql_free_result($result);
         return $res;
      }
	  
	  function RunQueryGetId($query)
      {
         $result = $this->RunQuery($query);
         $id = mysql_insert_id();
	//	 mysql_free_result($query); 
		 return $id;
      }
	  
     function ArrFromQuery($query)
     {
	      $arr = array();
          $result = $this->RunQuery($query);
          $i = 0;
          while ( $row = mysql_fetch_assoc($result) )
          {
              foreach ( $row as $key => $value )
              {
                 $arr[$i][$key] = $value;
              }
              $i++;
          }
 		  mysql_free_result($result); 
          return $arr;
      }
	  
	  function NumRows($query)
      {
         $rows = $this->RunQuery($query);
         return  mysql_num_rows($rows);
      }
	  
	 function IsEmpty($tbl,$type)
	 {
        $query = "select * from $tbl where $type<>'' order by id";
        $num = $this->NumRows($query);	   
	    return $num;
	 }
	
	 function MakeQuery($post,$arr=array(),$impl=", ")
	 {
	    $q1 = "";
	    $q = array();
	    foreach($post as $key=>$value) 
		    if ((!in_array($key,$arr)) && ($value!=""))
               $q[]=$key."='".$value."'";
 		if (count($q)>0)
		   $q1 = join($impl,$q);
		return $q1;
	 }
	 
	 function CloseConnect()
	 {
	   @mysql_close($this->db) or die (mysql_error());//or die ('Error'); 
	 }
	function __destruct()
{
	$this->CloseConnect();
}
  }
?>